package com.pruebas.physics.worldgravityandcollision;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import com.pruebas.physics.common.Circle;


import android.opengl.GLSurfaceView;
import android.opengl.GLU;

public class WorldGravityAndCollisionRenderer implements GLSurfaceView.Renderer {
	
	private static final float gravity = -0.15f;
	private static final float timeDelta = 0.5f;
	
	private static final int definition = 60;
	private Circle world;
	private Circle ball;
	
	public boolean touching = false;
	
	private float ballLastSpeed = 0f;
	private float ballLastXPos = 0f;
	private float ballLastYPos = 0f;
	
	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		// Set the background frame color
        gl.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
        // initialize the rectangle vertex array
        initShapes();
        
        // Enable use of vertex arrays
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    }
    
    public void onDrawFrame(GL10 gl) {
    	// Redraw background color
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
        
        // Set GL_MODELVIEW transformation mode
        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity();   // reset the matrix to its default state
        
        // When using GL_MODELVIEW, you must set the view point
        GLU.gluLookAt(gl, 0, 0, -5, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
        
    	float distanceX = ballLastXPos - world.getInitialX();
        float distanceY = ballLastYPos- world.getInitialY();
        double xyAngle = Math.atan(distanceX/distanceY);
        
        float ballSpeed = ballLastSpeed + gravity * timeDelta;
        
        float distance = (float)Math.sqrt((distanceX * distanceX) + (distanceY * distanceY));
        float newDistance = distance + ballSpeed * timeDelta;
        
        if(newDistance <= world.getRadius() + ball.getRadius()){
        	ballSpeed *= -0.8f;
        	newDistance = world.getRadius() + ball.getRadius();
        }
        
        float ballXPos = (float)(Math.sin(xyAngle) * newDistance) + world.getInitialX();
        float ballYPos = (float)(Math.cos(xyAngle) * newDistance) + world.getInitialY();
        
        ballLastSpeed = ballSpeed;
        ballLastXPos = ballXPos ;
        ballLastYPos = ballYPos ;
        
        //Ball
        gl.glPushMatrix();
        gl.glColor4f(0f, 1f, 1f, 0.0f);
        gl.glTranslatef(ballXPos - ball.getInitialX(), ballYPos - ball.getInitialY(), 0);
        ball.draw(gl);
        gl.glPopMatrix();
        
        //world
        gl.glPushMatrix();
        gl.glColor4f(1f, 0f, 1f, 0.0f);
        world.draw(gl);
        gl.glPopMatrix();
    }
    
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        gl.glViewport(0, 0, width, height);
        
        // make adjustments for screen ratio
        float ratio = (float) width / height;
        gl.glMatrixMode(GL10.GL_PROJECTION);        // set matrix to projection mode
        gl.glLoadIdentity();                        // reset the matrix to its default state
        gl.glFrustumf(-ratio, ratio, -1, 1, 1f, 10);  // apply the projection matrix

    }
    
    private void initShapes(){
    	ballLastXPos = -2.5f;
    	ballLastYPos = 3f;
    	ball = new Circle(0.5f, definition, ballLastXPos, ballLastYPos);
    	world = new Circle(2f, definition, 1f, -2.5f);
    }

}
